<?php

class ConfigAttributes extends Controller{

    private $_data;
    
    public function __construct(){
        parent::__construct();
        $this->_data = Array();
    }

    private function _render () {
        $this->_data['title'] = 'Gestión de atributos';
        $this->_data['page'] = 'configAttributes';
        
        $this->_view->rendertemplate('header',$this->_data);
        $this->_view->rendertemplate('menu',$this->_data);
        $this->_view->render('config/attributes',$this->_data);
        $this->_view->rendertemplate('footer',$this->_data);
    }
    
    public function index(){
        $this->_data['page_section'] = 'main';
        $this->_data['attributes'] = $this->_model->getAttributes ();
        $this->_render();
    }

    public function attributes () {
        $attributes = $this->_model->getAttributes ();  
        $this->_view->setOutput (json_encode ($attributes));
    }
    
    public function attribute () {
        $response = Array();
        $response['result'] = 'error';
        
        if (isset ($_GET['id'])) {
            $response['result'] = 'success';
            $response['attribute'] = $this->_model->getAttribute ($_GET['id']);  
        }
        
        $this->_view->setOutput (json_encode ($response));
    }
    
    public function addAttribute () {
        $response = Array();
        
        if (!isset ($_POST['name']) || !isset ($_POST['type']) || !isset ($_POST['defaultValue'])) {
            $response['result'] = 'error';
            $response['message'] = 'POST_PARAMS ('.json_encode ($_POST).')';
        }
        else {
            $name = $_POST['name'];
            $type = $_POST['type'];
            $defaultValue = $_POST['defaultValue'];
            
            if ($this->_model->addAttribute ($name, $type, $defaultValue) == true) {
                $response['result'] = 'success';
            }
            else {
                $response['result'] = 'error';
                $response['message'] = 'SQL FAIL';
            }
        }
        
        $this->_view->setOutput (json_encode ($response));
    }
    
    public function modifyAttribute () {
        $response = Array();
        
        if (!isset ($_POST['id']) || !isset ($_POST['name']) || !isset ($_POST['type']) || !isset ($_POST['defaultValue'])) {
            $response['result'] = 'error';
            $response['message'] = 'POST_PARAMS ('.json_encode ($_POST).')';
        }
        else {
            $id = $_POST['id'];
            $name = $_POST['name'];
            $type = $_POST['type'];
            $defaultValue = $_POST['defaultValue'];
            
            if ($this->_model->modifyAttribute ($id, $name, $type, $defaultValue) == true) {
                $response['result'] = 'success';
            }
            else {
                $response['result'] = 'error';
                $response['message'] = 'SQL FAIL';
            }
        }
        
        $this->_view->setOutput (json_encode ($response));
    }
    
    public function removeAttribute () {
        $response = Array();
        
        if (!isset ($_POST['id'])) {
            $response['result'] = 'error';
            $response['message'] = 'POST_PARAMS ('.json_encode ($_POST).')';
        }
        else {
            $id = $_POST['id'];
            
            if ($this->_model->removeAttribute ($id) == true) {
                $response['result'] = 'success';
            }
            else {
                $response['result'] = 'error';
                $response['message'] = 'SQL FAIL';
            }
        }
        
        $this->_view->setOutput (json_encode ($response));
    }

}

?>